package com.tidc.seaserverweb.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.UserPackage;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.exception.TidcRestException;
import com.tidc.seaserverservice.service.SecondaryPackageService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@RequestMapping("/secondary/package")
public class SecondaryPackageController {
    @Resource
    private SecondaryPackageService secondaryPackageService;

    /**
     * 获取自己的背包仓库
     * @param pageSize
     * @param pageNum
     * @param authentication
     * @return
     * @throws TidcBadParent
     */
    @GetMapping("/list")
    public Fres<Map<Object,Object>> getUserPackage(@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum,Authentication authentication) throws TidcBadParent {
        return secondaryPackageService.getUserPackage(pageSize,pageNum,authentication);
    }

    /**
     * 使用奖励
     * 1. 将使用的物品和数量放到待兑换列表
     * 2. 通知定时任务组件 建立任务
     * 3. 定时任务组件检查是否完成 未完成则设置成已过期状态
     * @param userPackage
     * @param authentication
     * @return
     */
    @PutMapping("/use/shop")
    public Fres<String> useShop(@RequestBody UserPackage userPackage,Authentication authentication) throws TidcRestException, JsonProcessingException {
        return secondaryPackageService.useShop(userPackage,authentication);
    }
}
